package com.sanshi.project.procure.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sanshi.project.admin.api.entity.ProcureApplyEntity;
import com.sanshi.project.procure.mapper.ProcureApplyDetailsMapper;
import com.sanshi.project.procure.mapper.ProcureApplyMapper;
import com.sanshi.project.procure.service.ProcureApplyService;
import org.springframework.stereotype.Service;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sanshi.project.admin.api.entity.ProcureApplyDetailsEntity;
import org.springframework.transaction.annotation.Transactional;
import lombok.RequiredArgsConstructor;
import java.util.Objects;
/**
 * 出入库申请单
 *
 * @author nikon
 * @date 2023-12-10 15:54:35
 */
@Service
@RequiredArgsConstructor
public class ProcureApplyServiceImpl extends ServiceImpl<ProcureApplyMapper, ProcureApplyEntity> implements ProcureApplyService {
  private final ProcureApplyDetailsMapper procureApplyDetailsMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveDeep(ProcureApplyEntity procureApply) {
        baseMapper.insert(procureApply);
        for (ProcureApplyDetailsEntity  procureApplyDetails : procureApply.getProcureApplyDetailsList()) {
            procureApplyDetails.setApplyId(procureApply.getId());
            procureApplyDetailsMapper.insert( procureApplyDetails);
        }

        return Boolean.TRUE;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateDeep(ProcureApplyEntity procureApply) {
        baseMapper.updateById(procureApply);
        for (ProcureApplyDetailsEntity  procureApplyDetails : procureApply.getProcureApplyDetailsList()) {
            if (Objects.isNull(procureApplyDetails.getId())) {
                procureApplyDetails.setApplyId(procureApply.getId());
                procureApplyDetailsMapper.insert(procureApplyDetails);
            } else {
                procureApplyDetailsMapper.updateById(procureApplyDetails);
            }
        }
        return Boolean.TRUE;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean removeDeep(Long[] ids) {
        baseMapper.deleteBatchIds(CollUtil.toList(ids));
        procureApplyDetailsMapper.delete(Wrappers.<ProcureApplyDetailsEntity>lambdaQuery().in(ProcureApplyDetailsEntity::getApplyId, ids));
        return Boolean.TRUE;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean removeChild(Long[] ids) {
        procureApplyDetailsMapper.deleteBatchIds(CollUtil.toList(ids));
        return Boolean.TRUE;
    }
}